package net.gd.service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.gd.domain.dbManage;
import net.gd.domain.dbManageExample;
import net.gd.domain.dbPermissionDomain;
import net.gd.domain.dbPermissionDomainExample;
import net.gd.domain.dbPermissionLog;
import net.gd.domain.modelManage;
import net.gd.domain.modelManageExample;
import net.gd.domain.roleDetail;
import net.gd.domain.roleDetailExample;
import net.gd.domain.roleTopicRel;
import net.gd.domain.roleTopicRelExample;
import net.gd.domain.topicDetail;
import net.gd.domain.topicDetailExample;
import net.gd.dto.dbManageDto;
import net.gd.dto.dbPermissionDto;
import net.gd.dto.roleTopicRelDto;
import net.gd.dto.topicDetailDto;
import net.gd.persistence.dbManageMapper;
import net.gd.persistence.dbPermissionDomainMapper;
import net.gd.persistence.dbPermissionLogMapper;
import net.gd.persistence.modelManageMapper;
import net.gd.persistence.roleDetailMapper;
import net.gd.persistence.roleTopicRelMapper;
import net.gd.persistence.topicDetailMapper;

/** 
 * @author Uriel 
 * @date 2018年2月5日 上午12:22:02 
 * @version 1.0 
*/
@Service
public class dbPermissionService {
	@Autowired 
	roleDetailMapper roleDetailMapper;
	
	@Autowired
	roleTopicRelMapper roleTopicMapper;
	
	@Autowired
	topicDetailMapper topicMapper;
	
	@Autowired
	dbPermissionDomainMapper dbPermissionMapper;
	
	@Autowired
	dbPermissionLogMapper dbPermissionLogMapper;
	
	@Autowired
	dbManageMapper dbMapper;
	
	@Autowired
	modelManageMapper modelMapper;
	
	/**
	 * @description 保存角色 
	 * @author Uriel
	 * @date 2018年2月5日 上午12:27:01
	 * @version V3.0
	 * @param roleDetail
	 */
	public void saveRole(roleDetail roleDetail){
		roleDetailMapper.insert(roleDetail);
	}
	
	/**
	 * @description 根据角色Id查询角色-主题关系 
	 * @author Uriel
	 * @date 2018年2月5日 上午12:29:41
	 * @version V3.0
	 * @param roleId
	 * @return
	 */
	public List<roleTopicRel> selectRoleTopicListByRole(String roleId){
		roleTopicRelExample roleTopic = new roleTopicRelExample();
		roleTopic.createCriteria().andRoleidEqualTo(roleId);
		List<roleTopicRel> roleTopicList = roleTopicMapper.selectByExample(roleTopic);
		return roleTopicList;
	}
	
	/**
	 * @description 删除角色 
	 * @author Uriel
	 * @date 2018年2月6日 下午1:54:08
	 * @version V3.0
	 * @param roleId
	 */
	public void deleteRoleTopicRel(String roleId){
		roleTopicRelExample roleTopic = new roleTopicRelExample();
		roleTopic.createCriteria().andRoleidEqualTo(roleId);
		roleTopicMapper.deleteByExample(roleTopic);
	}
	
	/**
	 * @description 根据主题Id查询角色-主题关系 
	 * @author Uriel
	 * @date 2018年2月5日 上午12:45:46
	 * @version V3.0
	 * @param topicID
	 * @return
	 */
	public List<roleTopicRel> selectRoleTopicListByTopic(String topicID){
		roleTopicRelExample roleTopicExample = new roleTopicRelExample();
		roleTopicExample.createCriteria().andTopicidEqualTo(topicID);
		List<roleTopicRel> roleTopicList = roleTopicMapper.selectByExample(roleTopicExample);
		return roleTopicList;
	}
	
	/**
	 * @description 多条件查询角色-主题关系 
	 * @author Uriel
	 * @date 2018年2月5日 上午12:47:47
	 * @version V3.0
	 * @param roleId
	 * @param topicId
	 * @return
	 */
	public roleTopicRel selectRoleTopicList(String roleId, String topicId){
		roleTopicRelDto roleTopicDto = new roleTopicRelDto();
		roleTopicDto.setRoleid(roleId);
		roleTopicDto.setTopicid(topicId);
		roleTopicRel roleTopic = roleTopicMapper.selectByParam(roleTopicDto);
		return roleTopic;
	}
	
	/**
	 * @description 删除角色 
	 * @author Uriel
	 * @date 2018年2月5日 上午12:30:22
	 * @version V3.0
	 * @param roleId
	 */
	public void deleteRole(String roleId){
		roleDetailExample roleExample = new roleDetailExample();
		roleExample.createCriteria().andRoleidEqualTo(roleId);
		roleDetailMapper.deleteByExample(roleExample);
	}
	
	/**
	 * @description 删除角色-主题关系 
	 * @author Uriel
	 * @date 2018年2月6日 下午2:26:53
	 * @version V3.0
	 * @param id
	 */
	public void deleteRoleToleRel(String id){
		roleTopicRelExample roleTopicExample = new roleTopicRelExample();
		roleTopicExample.createCriteria().andIdEqualTo(id);
		roleTopicMapper.deleteByExample(roleTopicExample);
	}
	
	/**
	 * @description 查询角色 
	 * @author Uriel
	 * @date 2018年2月5日 上午12:31:26
	 * @version V3.0
	 * @param roleId
	 * @return
	 */
	public List<roleDetail> selectRole(String roleId){
		roleDetailExample roleExample = new roleDetailExample();
		if(!StringUtils.isEmpty(roleId)){
			roleExample.createCriteria().andRoleidEqualTo(roleId);
		}
		List<roleDetail> roleList = roleDetailMapper.selectByExample(roleExample);
		return roleList;
	}
	
	/**
	 * @description 更新角色 
	 * @author Uriel
	 * @date 2018年2月5日 上午12:33:20
	 * @version V3.0
	 * @param roleId
	 * @param roleName
	 * @param roleDesc
	 * @param time
	 */
	public void updateRole(String roleId, String roleName, String roleDesc, String time){
		roleDetailExample roleExample = new roleDetailExample();
		roleExample.createCriteria().andRoleidEqualTo(roleId);
		roleDetailMapper.deleteByExample(roleExample);
		roleDetail roleDetail = new roleDetail();
		roleDetail.setRoleid(roleId);
		roleDetail.setRolename(roleName);
		roleDetail.setRoledesc(roleDesc);
		roleDetail.setCreatetime(time);
		roleDetailMapper.insert(roleDetail);
	}
	
	/**
	 * @description 查询主题 
	 * @author Uriel
	 * @date 2018年2月5日 上午12:34:37
	 * @version V3.0
	 * @param topicId
	 * @return
	 */
	public List<topicDetail> selectTopicList(String topicId){
		topicDetailExample topicExample = new topicDetailExample();
		topicExample.createCriteria().andTopicidEqualTo(topicId);
		List<topicDetail> topicList = topicMapper.selectByExample(topicExample);
		return topicList;
	}
	
	/**
	 * @description 根据主题名称、主题类型查询主题明细 
	 * @author Uriel
	 * @date 2018年2月6日 下午4:02:13
	 * @version V3.0
	 * @param topicType
	 * @param topicName
	 * @return
	 */
	public topicDetail selectTopicList(String topicType, String topicName){
		topicDetailDto topicDto = new topicDetailDto();
		topicDto.setTopicname(topicName);
		topicDto.setTopictype(topicType);
		topicDetail topicList = topicMapper.selectByParams(topicDto);
		return topicList;
	}
	
	/**
	 * @description 新增主题 
	 * @author Uriel
	 * @date 2018年2月5日 上午12:36:03
	 * @version V3.0
	 * @param topicDetail
	 */
	public void addTopic(topicDetail topicDetail){
		topicMapper.insert(topicDetail);
	}
	
	/**
	  * @description 新增角色-主题关系 
	 * @author Uriel
	 * @date 2018年2月5日 上午12:40:14
	 * @version V3.0
	 * @param roleTopic
	 */
	public void addRoleTopicRel(roleTopicRel roleTopic){
		roleTopicMapper.insert(roleTopic);
	}
	
	
	/**
	 * @description 获得主题-表关系 
	 * @author Uriel
	 * @date 2018年2月5日 上午12:42:22
	 * @version V3.0
	 * @param topicID
	 * @return
	 */
	public List<dbPermissionDomain> selectTopicTablRel(String topicID){
		dbPermissionDomainExample dbPermissionExample = new dbPermissionDomainExample();
		dbPermissionExample.createCriteria().andTopicidEqualTo(topicID);
		List<dbPermissionDomain> dbPermissionList = dbPermissionMapper.selectByExample(dbPermissionExample);
		return dbPermissionList;
	}
	
	/**
	 * @description 删除主题 
	 * @author Uriel
	 * @date 2018年2月5日 上午12:50:18
	 * @version V3.0
	 * @param topicId
	 */
	public void deleteTopic(String topicId){
		topicDetailExample topicExample = new topicDetailExample();
		topicExample.createCriteria().andTopicidEqualTo(topicId);
		topicMapper.deleteByExample(topicExample);
	}
	
	/**
	 * @description 新建主题 
	 * @author Uriel
	 * @date 2018年2月5日 上午2:23:40
	 * @version V3.0
	 * @param topicID
	 * @param topicName
	 * @param topicTp
	 * @param topicDesc
	 * @param time
	 */
	public void addTopic(String topicID, String topicName, String topicTp, String topicDesc, String time){
		topicDetail topicDetail = new topicDetail();
		topicDetail.setTopicid(topicID);
		topicDetail.setTopicname(topicName);
		topicDetail.setTopictype(topicTp);
		topicDetail.setTopicdesc(topicDesc);
		topicDetail.setCreatetime(time);
		topicMapper.insert(topicDetail);
	}
	
	/**
	 * @description 添加主题-表关系 
	 * @author Uriel
	 * @date 2018年2月5日 上午3:04:57
	 * @version V3.0
	 * @param dbPermissionDomain
	 */
	public void addTopicTblRel(dbPermissionDomain dbPermissionDomain){
		dbPermissionMapper.insert(dbPermissionDomain);
	}
	
	/**
	 * @description 根据主键删除主题-表关系 
	 * @author Uriel
	 * @date 2018年2月5日 上午3:20:53
	 * @version V3.0
	 * @param id
	 */
	public void deleteTopicTblRel(int id){
		dbPermissionDomainExample dbExample = new dbPermissionDomainExample();
		dbExample.createCriteria().andIdEqualTo(id);
		dbPermissionMapper.deleteByExample(dbExample);
	}
	
	/**
	 * @description 根据角色Id获得主题相关信息
	 * @author Uriel
	 * @date 2018年2月5日 上午3:35:01
	 * @version V3.0
	 * @param roleId
	 * @return
	 */
	public List<topicDetail> selectTopicIdList(String roleId){
		roleTopicRelExample roleTopicRel = new roleTopicRelExample();
		if(!"0".equals(roleId)){
			roleTopicRel.createCriteria().andRoleidEqualTo(roleId);
		}else{
			roleTopicRel.createCriteria().andRoleidIsNotNull();
		}
		List<roleTopicRel> roleTopicList = roleTopicMapper.selectByExample(roleTopicRel);
		
		List<topicDetail> topicDetailList = new ArrayList<topicDetail>();
		
		for(roleTopicRel roleTopic : roleTopicList){
			topicDetailExample topicExample = new topicDetailExample();
			String topicId = roleTopic.getTopicid();
			topicExample.createCriteria().andTopicidEqualTo(topicId);
			topicDetail topic = topicMapper.selectOneByExample(topicExample);
			if(topic != null){
				topicDetailList.add(topic);
			}
		}
		return topicDetailList;
		
	}
	
	/**
	 * @description 根据角色Id和主题类型获得主题信息 
	 * @author Uriel
	 * @date 2018年2月5日 上午3:45:11
	 * @version V3.0
	 * @param roleId
	 * @param topicType
	 * @return
	 */
	public List<topicDetail> selectTopicIdList(String roleId, String topicType){
		roleTopicRelExample roleTopicRel = new roleTopicRelExample();
		if(!"0".equals(roleId)){
			roleTopicRel.createCriteria().andTopicidEqualTo(topicType);
		}
		List<roleTopicRel> roleTopicList = roleTopicMapper.selectByExample(roleTopicRel);
		
		List<topicDetail> topicDetailList = new ArrayList<topicDetail>();
		
		for(roleTopicRel roleTopic : roleTopicList){
			String topicId = roleTopic.getTopicid();
			topicDetailDto topicDto = new topicDetailDto();
			topicDto.setTopicid(topicId);
			topicDto.setTopictype(topicType);
			topicDetail topic = topicMapper.selectByParam(topicDto);
			if(topic != null){
				topicDetailList.add(topic);
			}
		}
		return topicDetailList;
	}
	
	/**
	 * @description 新增表-权限 
	 * @author Uriel
	 * @date 2018年2月5日 上午4:11:34
	 * @version V3.0
	 * @param dbPermissionDomain
	 */
	public void addDbPermission(dbPermissionDomain dbPermissionDomain){
		dbPermissionMapper.insert(dbPermissionDomain);
	}
	
	/**
	 * @description 根据id获得表-权限关系 
	 * @author Uriel
	 * @date 2018年2月5日 上午4:19:16
	 * @version V3.0
	 * @param id
	 * @return
	 */
	public List<dbPermissionDomain> selectDbPermissionById(int id){
		dbPermissionDomainExample dbPermission = new dbPermissionDomainExample();
		dbPermission.createCriteria().andIdEqualTo(id);
		List<dbPermissionDomain> dbPermissionList = dbPermissionMapper.selectByExample(dbPermission);
		return dbPermissionList;
	}
	
	/**
	 * @description 根据主题Id，数据库名称，数据表名称查询主题-表权限关系 
	 * @author Uriel
	 * @date 2018年2月7日 上午9:25:13
	 * @version V3.0
	 * @param topicId
	 * @param dbName
	 * @param tbName
	 * @return
	 */
	public dbPermissionDomain selectDbPermission(String topicId, String dbName, String tbName){
		dbPermissionDto dbPermission = new dbPermissionDto();
		dbPermission.setTopicid(topicId);
		dbPermission.setDbname(dbName);
		dbPermission.setTablename(tbName);
		dbPermissionDomain dbPermissionInfo = dbPermissionMapper.selectByParams(dbPermission);
		return dbPermissionInfo;
	}
	
	/**
	 * @description 根据id删除表-权限关系 
	 * @author Uriel
	 * @date 2018年2月5日 上午4:21:46
	 * @version V3.0
	 * @param id
	 */
	public void deleteDbPermissionById(int id){
		dbPermissionDomainExample dbPermission = new dbPermissionDomainExample();
		dbPermission.createCriteria().andIdEqualTo(id);
		dbPermissionMapper.deleteByExample(dbPermission);
	}
	
	/**
	 * @description 获得当前所有角色信息 
	 * @author Uriel
	 * @date 2018年2月5日 上午4:36:51
	 * @version V3.0
	 * @return
	 */
	public List<roleDetail> selectRoles(){
		roleDetailExample roleExample = new roleDetailExample();
		List<roleDetail> roleInfo = roleDetailMapper.selectByExample(roleExample);
		return roleInfo;
	}
	
	/**
	 * @description 根据主题类型，获得所有角色信息 
	 * @author Uriel
	 * @date 2018年2月5日 上午4:44:11
	 * @version V3.0
	 * @param topicType
	 * @return
	 */
	public List<roleDetail> selectRoles(String topicType){
		topicDetailExample topicExample = new topicDetailExample();
		topicExample.createCriteria().andTopictypeEqualTo(topicType);
		List<topicDetail> topicList = topicMapper.selectByExample(topicExample);
		
		List<roleTopicRel> roleTopicList  = new ArrayList<roleTopicRel>();
		for(topicDetail topic : topicList){
			String topicId = topic.getTopicid();
			roleTopicRelExample roleTopicExample = new roleTopicRelExample();
			roleTopicExample.createCriteria().andTopicidEqualTo(topicId);
			roleTopicRel roleDetailList = roleTopicMapper.selectOneByExample(roleTopicExample);
			roleTopicList.add(roleDetailList);
		}
		
		List<roleDetail> roleList = new ArrayList<roleDetail>();
		for(roleTopicRel role : roleTopicList){
			String roleId = role.getId();
			roleDetailExample roleExample = new roleDetailExample();
			roleExample.createCriteria().andRoleidEqualTo(roleId);
			roleDetail roleInfo = roleDetailMapper.selectOneByExample(roleExample);
			roleList.add(roleInfo);
		}
		
		return roleList;
	}
	
	/**
	 * @description 根据主题Id获得表-权限关系 
	 * @author Uriel
	 * @date 2018年2月5日 上午5:08:52
	 * @version V3.0
	 * @param topicId
	 * @return
	 */
	public List<dbPermissionDomain> selectDbPermission(String topicId){
		dbPermissionDomainExample dbPermissionExample = new dbPermissionDomainExample();
		if(!StringUtils.isEmpty(topicId)){
			dbPermissionExample.createCriteria().andTopicidEqualTo(topicId);
		}
		List<dbPermissionDomain> dbPermissionList = dbPermissionMapper.selectByExample(dbPermissionExample);
		return dbPermissionList;
	}
	
	/**
	 * @description 获得角色信息 
	 * @author Uriel
	 * @date 2018年2月5日 下午2:12:48
	 * @version V3.0
	 * @param roleName
	 */
	public List<roleDetail> selectRoleDetail(String roleName){
		roleDetailExample roleExample = new roleDetailExample();
		roleExample.createCriteria().andRolenameEqualTo(roleName);
		List<roleDetail> roleList = roleDetailMapper.selectByExample(roleExample);
		return roleList;
	}
	
	/**
	 * @description 根据角色名称删除记录 【更新记录】
	 * @author Uriel
	 * @date 2018年2月5日 下午2:15:05
	 * @version V3.0
	 * @param roleName
	 */
	public void deleteRoleDetail(String roleName){
		roleDetailExample roleExample = new roleDetailExample();
		roleExample.createCriteria().andRolenameEqualTo(roleName);
		roleDetailMapper.deleteByExample(roleExample);
	}
	
	/**
	 * @description 根据表名和库名排除检索记录 
	 * @author Uriel
	 * @date 2018年2月7日 上午2:37:17
	 * @version V3.0
	 * @param dbName
	 * @param tbName
	 * @return
	 */
	public List<dbManage> selectDbList(String dbName, String tbName){
		dbManageDto dbDto = new dbManageDto();
		dbDto.setDatabaseName(dbName);
		dbDto.setSourceTable(tbName);
		List<dbManage> dbInfo = dbMapper.selectByParams(dbDto);
		return dbInfo;
	}
	
	/**
	 * @description 获得所有数据库-表信息 
	 * @author Uriel
	 * @date 2018年2月7日 上午2:45:08
	 * @version V3.0
	 * @return
	 */
	public List<dbManage> selectDbList(){
		dbManageExample dbExample = new dbManageExample();
		List<dbManage> dbInfo = dbMapper.selectByExample(dbExample);
		return dbInfo;
	}
	
	/**
	 * @description 查询全部模型数据 
	 * @author Uriel
	 * @date 2018年2月8日 下午3:48:15
	 * @version V3.0
	 * @return
	 */
	public List<modelManage> selectModelList(){
		modelManageExample modelExample = new modelManageExample();
		List<modelManage> modelList = modelMapper.selectByExample(modelExample);
		return modelList;
	}
	
	/**
	 * @description 获得最后输出表的模型记录 
	 * @author Uriel
	 * @date 2018年2月8日 下午6:11:56
	 * @version V3.0
	 * @return
	 */
	public List<modelManage> selectDistinctModel(){
		List<modelManage> modelMaxList = new ArrayList<modelManage>();
		
		modelManageExample modelExample = new modelManageExample();
		modelExample.createCriteria().andDelFlgEqualTo("0");
		List<modelManage> modelList = modelMapper.selectByExample(modelExample);
		
		// 获得有效的模型名称
		List<String> modelIdList = new ArrayList<String>();
		for(int i=0;i<modelList.size();i++){
			modelManage model = modelList.get(i);
			String modelId = model.getModeId();
			if(modelIdList.indexOf(modelId) == -1 && !StringUtils.isEmpty(modelId)){
				modelIdList.add(modelId);
			}
		}
		
		// 根据模型名称获得每个模型的最后输出记录
		for(int j=0;j<modelIdList.size();j++){
			modelManageExample modelEx = new modelManageExample();
;			modelEx.createCriteria().andModeIdEqualTo(modelIdList.get(j));
			int id = modelMapper.selectMaxId(modelEx);
			modelEx.createCriteria().andIdEqualTo(id);
			modelManage modelMax = modelMapper.selectByPrimaryKey(id);
			if(modelMax != null){
				modelMaxList.add(modelMax);
			}
		}
		
		return modelMaxList;
	}
	
	/**
	 * @description 插入日志 
	 * @author Uriel
	 * @date 2018年2月5日 上午2:14:07
	 * @version V3.0
	 * @param roleId
	 * @param userId
	 * @param opType
	 * @param time
	 * @param status
	 * @param message
	 */
	public void insertLog(String operRole, String userId, String opType, String time, String status, String  message){
		dbPermissionLog dbPermissionLog = new dbPermissionLog();
		dbPermissionLog.setId(UUID.randomUUID().toString());
		dbPermissionLog.setRoleid(operRole);
		dbPermissionLog.setUserid(userId);
		dbPermissionLog.setOptype(opType);
		dbPermissionLog.setOptime(time);
		dbPermissionLog.setStatus(status);
		dbPermissionLog.setDetails(message);
		dbPermissionLogMapper.insert(dbPermissionLog);
	}
	
}
